<?php
class Db
{
	var $_server = null;
	var $_schema = null;
	var $_user = null;
	var $_password = null;

	var $_link = null;

	var $_sql = null;

	function __construct($server, $schema, $user, $password)
	{
		$this->_server = $server;
		$this->_schema = $schema;
		$this->_user = $user;
		$this->_password = $password;
	}

	function connect() {
		$this->_link = mysql_connect($this->_server, $this->_user, $this->_password);
		mysql_select_db($this->_schema, $this->_link);
	}

	function getTables($like='') {
		if($like) {
			$like = " like '$like%'";
			$field = "Tables_in_" .$this->_schema ;
			$where = " where " . $field . $like;
		}

		$sql = "show tables " . $where;

		$this->_sql = $sql;
		$result = mysql_query($sql, $this->_link);

		$rows = mysql_num_rows($result);
		$tbList = array();
		while ($row = mysql_fetch_assoc($result)) {
			$tbList[] = $row[$field];
		}
		mysql_free_result($result);

		return $tbList;
	}

	function getCount($table, $where) {
		$sql = "SELECT count(*) FROM $table" . " $where";
		$this->_sql = $sql;

		$result = mysql_query($sql, $this->_link);
		$rows = mysql_num_rows($result);
		while ($row = mysql_fetch_assoc($result)) {
			$cnt = $row['count(*)'];
		}
		mysql_free_result($result);

		return $cnt;
	}

	function delete($table, $where) {
		$sql = "delete FROM $table" . " $where";
		$this->_sql = $sql;

		$result = mysql_query($sql, $this->_link);

		return mysql_affected_rows($this->_link);

	}

	function close(){
		mysql_close($this->_link);
	}



}